package com.cssw.fyzb.busi.api.text;


import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.core.model.vo.text.TextKindVo;
import com.cssw.fyzb.busi.service.text.TextKindService;
import com.cssw.fyzb.core.model.dodb.SysText;
import com.cssw.fyzb.core.util.StringUtil;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.simplecache.SimpleCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Auther: zzf
 * @Date: 2020/12/15 14:40
 * @Description:
 */
@Function(description = "文本内容查询", parameters = {
        @Parameter(name = "textCode", type = Type.String, description = "文本编码", required = true),
})
@Component
public class GetSysText implements Action {

    private final static Logger logger = LoggerFactory.getLogger(GetSysText.class);

    private static final String TEXT_CODE_CONTENT = "textCodeContent";

    @Autowired
    private SimpleCache simpleCache;

    @Autowired
    private TextKindService testKindService;

    @Override
    public Response execute(Request request) throws Exception {
        try {
            String textCode = request.getString("textCode");
            String cacheText =  simpleCache.get(TEXT_CODE_CONTENT + textCode);

            if (StringUtil.isNotEmpty(cacheText)){
                return Response.success().put("data", JSON.parseObject(cacheText, TextKindVo.class));
            }
            SysText text = testKindService.getSysText(textCode);
            TextKindVo vo = new TextKindVo();
            vo.setTextCode(text.getTextCode());
            vo.setTextName(text.getTextName());
            vo.setKindCode(text.getKindCode());
            vo.setContent(text.getContent());
            vo.setUpdateOperId(text.getUpdateOperId());
            vo.setUpdateTime(text.getUpdateTime());
            vo.setCreateTime(text.getCreateTime());
            vo.setCreateOperId(text.getCreateOperId());
            vo.setTextUrl(text.getTextUrl());
            simpleCache.put(TEXT_CODE_CONTENT + textCode, JSON.toJSONString(vo));
            return Response.success().put("data", vo);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("查询失败");
        }
    }
}
